package com.ljh.SimpleTradingSystem.controller;

import com.ljh.SimpleTradingSystem.mybatis.entity.User;
import com.ljh.SimpleTradingSystem.mybatis.mapper.UserMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Controller
public class LoginController {
    @Resource
    HttpServletRequest req;

    @Resource
    UserMapper userMapper;

    @GetMapping("/login")
    public String loginForm() {
        String userId = String.valueOf(req.getSession().getAttribute("userId"));
        if (!userId.equals("null")) {
            return "GoodsList";
        }
        return "Login";
    }

    @PostMapping("/login")
    @ResponseBody
    public String login(@RequestParam String userName, @RequestParam String password) {
        User user = userMapper.selectByName(userName);
        if (user == null) {
            return "用户名不存在";
        }

        String unlockTime = user.getUnlockTime();
        Date time = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = sdf.format(time);

        if (unlockTime.equals("null")) {
            if (!user.getPassword().equals(password)) {
                int errorNum = user.getErrorNum();
                if (errorNum == 3) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(time);
                    calendar.add(Calendar.HOUR, 1);
                    unlockTime = sdf.format(calendar.getTime());
                    userMapper.updateByName(userName, 0);
                    userMapper.updateUnlockTimeByName(userName, unlockTime);
                    return "密码输入错误3次，1小时后解锁账号";
                } else {
                    userMapper.updateByName(userName, errorNum + 1);
                    return "密码错误";
                }
            } else {
                HttpSession session = req.getSession();
                session.setAttribute("userId", user.getId());
                userMapper.updateByName(userName, 0);
                return "登录成功";
            }
        } else {
            if (unlockTime.compareTo(nowTime) > 0) {
                try {
                    long milliSeconds = (sdf.parse(unlockTime).getTime()) - time.getTime();
                    int minute = (int) milliSeconds / 1000 / 60;
                    return "密码输入错误3次，" + minute + "分钟后解锁账号";
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            } else {
                userMapper.updateUnlockTimeByName(userName, "null");
                HttpSession session = req.getSession();
                session.setAttribute("userId", user.getId());
            }
        }
        return "登录成功";
    }
}
